package com.hqyj.smvc.handler;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.smvc.pojo.Company;
import com.hqyj.smvc.pojo.Employee;
import com.hqyj.smvc.service.CompanyService;
import com.hqyj.smvc.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@Slf4j
public class CompanyHandler {
    @Autowired
    @Qualifier("companyService")
    private CompanyService companyService;
    @Autowired
    @Qualifier("employeeService")
    private EmployeeService employeeService;


    //公司信息分页展示
    @RequestMapping(value = "findAllCompany.action", method = RequestMethod.GET)
    public void findAllCompany(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("CompanyHandler>>>findAllCompany,start-------");
        int pageNum = 1;
        String pageNum1 = request.getParameter("pageNum");
        if(pageNum1 != null) {
            pageNum = Integer.parseInt(pageNum1);
        }
        List<Company> companies = companyService.exhibitionCompanyList(pageNum);
        PageInfo<Company> companyPageInfo = new PageInfo<>(companies);
        request.setAttribute("companies", companies);
        request.setAttribute("companyPageInfo", companyPageInfo);
        request.getRequestDispatcher("/WEB-INF/company/findAllCompany.jsp").forward(request, response);
        log.info("CompanyHandler>>>findAllCompany,end-------");
    }

    //点击查看详情展示公司雇员信息
    @RequestMapping(value = "allEmployees.action", method = RequestMethod.GET)
    public String allEmployees(String userid, HttpServletRequest request) {
        log.info("CompanyHandler>>>allEmployees,start-------");
        int i = Integer.parseInt(userid);
        List<Employee> employeeList = employeeService.companyEmployee(i);
        request.setAttribute("employeeList", employeeList);
        log.info("CompanyHandler>>>allEmployees,end-------");
        return "/WEB-INF/company/companyEmployee.jsp";
    }

    //点击修改跳转页面
    @RequestMapping(value = "jumpCompanyPage.action", method = RequestMethod.GET)
    public void jumpCompanyPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("CompanyHandler>>>jumpCompanyPage,start-------");
        String userid = request.getParameter("userid");
        int company_id = Integer.parseInt(userid);
        Company company = companyService.idCompany(company_id);
        request.setAttribute("company", company);
        request.getRequestDispatcher("/WEB-INF/company/modificationOfInformation.jsp").forward(request, response);
        log.info("CompanyHandler>>>jumpCompanyPage,end-------");
    }

    //变更公司信息
    @RequestMapping(value = "companyInformationChanges.action", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
    public void companyInformationChanges(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("CompanyHandler>>>companyInformationChanges,start-------");
        String company_id = request.getParameter("company_id");
        int parseInt = Integer.parseInt(company_id);
        String company_name = request.getParameter("company_name");
        String company_address = request.getParameter("company_address");
        Company company = new Company();
        company.setCompany_id(parseInt);
        company.setCompany_name(company_name);
        company.setCompany_address(company_address);
        companyService.companyChange(company);
        findAllCompany(request, response);
        log.info("CompanyHandler>>>companyInformationChanges,end-------");
    }

    //公司删除带雇员
    @RequestMapping(value = "theCompanyDisappeared.action", method = RequestMethod.GET)
    public void theCompanyDisappeared(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("CompanyHandler>>>theCompanyDisappeared,start-------");
        String company_id = request.getParameter("company_id");
        int parseInt = Integer.parseInt(company_id);
        System.out.println(parseInt);
        companyService.theCollapseOf(parseInt);
        findAllCompany(request, response);
        log.info("CompanyHandler>>>theCompanyDisappeared,end-------");

    }

    //添加按钮跳转页面
    @RequestMapping(value = "appendCompanyJump.action", method = RequestMethod.GET)
    public String appendCompanyJump(){
        return "/WEB-INF/company/createACompany.jsp";
    }

    //添加一个新的公司，同时至少添加一个雇员到此公司
    @RequestMapping(value = "appendCompany.action", method = RequestMethod.POST)
    public void appendCompany(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String company_name = request.getParameter("company_name");
        String company_address = request.getParameter("company_address");
        String employee_person = request.getParameter("employee_person");
        if (!("".equals(company_name) || "".equals(company_address))) {

            System.out.println(company_name);
            System.out.println(company_address);
            System.out.println(employee_person);

            Company company = new Company();
            company.setCompany_name(company_name);
            company.setCompany_address(company_address);
            companyService.createACompany(company, employee_person);

            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("添加成功");
        } else {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("添加失败，请检查");
        }
    }
}
